package warbots.entities.units;

import java.util.Arrays;

import warbots.gameplay.Player;
import warbots.gameplay.exceptions.CreateUnitException;
import warbots.gameplay.exceptions.GameplayException;

/**
 * @author Ido Adler.
 */
public class UnitFactory
{
    /**
     * TODO: description
     * 
     * @param unit
     * @param actions
     * @throws GameplayException
     */
    public static Unit newInstance(final Player p, final String unit, final String... actions)
        throws CreateUnitException
    {
    	// TODO: handle direction
    	Unit created;
        if (unit.equals("ARCHER"))
        {
            created = new Archer(p, unit, 0, null, actions);
        }
        else if (unit.equals("SOLDIER"))
        {
           created = new Soldier(p, unit, 0, null, actions);
        }
        else
        	created = new Unit(p, unit, actions);
        
        // TODO: move money handling outside of factory
        try
        {
            p.withdraw(created.getPrice());
        } catch (GameplayException e)
        {
            new CreateUnitException(e);
        }
        System.out.println("created unit: "+created.getName()+" of " +created.getClass());
        System.out.println("unit actions: "+Arrays.toString(created.getActionNames()));
        return created;
    }
}
